Option Strict Off
Option Explicit On
Friend Class SiteServerConnection
	
	Private mflgConnected As Boolean
	Private mobjWMI As WbemScripting.SWbemServices
	Private mstrSiteServerName As String
	Private mstrProviderServerName As Object
	Private mstrSiteCode As String
	Private mlngLastError As Integer
	Private mstrLastErrorString As String
	
	Public Event NewConnectionStatus(ByRef Connected As Boolean)
	
	Public Function SMSServices() As WbemScripting.SWbemServices
		SMSServices = mobjWMI
	End Function
	Public ReadOnly Property FullNamespace() As String
		Get
			'UPGRADE_WARNING: Couldn't resolve default property of object mstrProviderServerName. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			FullNamespace = "\\" & mstrProviderServerName & "\root\sms\site_" & mstrSiteCode
		End Get
	End Property
	
	Public ReadOnly Property Connected() As Boolean
		Get
			Connected = mflgConnected
		End Get
	End Property
	Public ReadOnly Property ProviderServerName() As String
		Get
			'UPGRADE_WARNING: Couldn't resolve default property of object mstrProviderServerName. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			ProviderServerName = mstrProviderServerName
		End Get
	End Property
	Public Property SiteServerName() As String
		Get
			SiteServerName = mstrSiteServerName
		End Get
		Set(ByVal Value As String)
			mstrSiteServerName = MyTrim(Value)
		End Set
	End Property
	Public Property SiteCode() As String
		Get
			SiteCode = mstrSiteCode
		End Get
		Set(ByVal Value As String)
            mstrSiteCode = Left(MyTrim(Value), 3)
		End Set
	End Property
	
	Public ReadOnly Property LastError() As Integer
		Get
			LastError = mlngLastError
		End Get
	End Property
	Public ReadOnly Property LastErrorString() As String
		Get
			LastErrorString = mstrLastErrorString
		End Get
	End Property
	
	Public Function SMSVersion() As String
		
		Dim objItems As WbemScripting.SWbemObjectSet
		Dim objItem As WbemScripting.SWbemObject
        Dim strTemp As String = ""
		On Error GoTo SMSVersionError
		objItems = mobjWMI.ExecQuery("SELECT Version from SMS_Site where SiteCode='" & mstrSiteCode & "'")
		For	Each objItem In objItems
			strTemp = objItem.Properties_.Item("Version").Value
			Exit For
		Next objItem
		SMSVersion = strTemp
		Exit Function
SMSVersionError: 
		SMSVersion = ""
	End Function
	
	Public Function Connect(Optional ByRef strUser As String = "", Optional ByRef strPassword As String = "") As Boolean
		
		On Error GoTo ConnectErr
		
		mlngLastError = 0
		mstrLastErrorString = ""
		
		Dim objLocator As WbemScripting.SWbemLocator
		Dim objTempServices As WbemScripting.SWbemServices
		
		objLocator = CreateObject("WbemScripting.SWbemLocator")
		If Len(strUser) > 0 Then
			objTempServices = objLocator.ConnectServer(mstrSiteServerName, "root\sms", strUser, strPassword)
		Else
			objTempServices = objLocator.ConnectServer(mstrSiteServerName, "root\sms")
		End If
		
		Dim colProviders As WbemScripting.SWbemObjectSet
		Dim objProvider As WbemScripting.SWbemObject
        Dim strNamespace As String = ""
		
		' Now figure out the site code for this server.
		colProviders = objTempServices.ExecQuery("SELECT * FROM SMS_ProviderLocation WHERE ProviderForLocalSite=true")
		
		For	Each objProvider In colProviders
			'UPGRADE_WARNING: Couldn't resolve default property of object objProvider.Machine. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			'UPGRADE_WARNING: Couldn't resolve default property of object mstrProviderServerName. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			mstrProviderServerName = objProvider.Machine
			'UPGRADE_WARNING: Couldn't resolve default property of object objProvider.NamespacePath. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			strNamespace = Mid(objProvider.NamespacePath, InStr(3, objProvider.NamespacePath, "\") + 1)
			mstrSiteCode = Mid(strNamespace, InStr(1, strNamespace, "_", CompareMethod.Binary) + 1)
			Exit For
		Next objProvider
		'UPGRADE_NOTE: Object objTempServices may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		objTempServices = Nothing
		
		' Finally, connect to the true provider location.
		If Len(strUser) > 0 Then
			'UPGRADE_WARNING: Couldn't resolve default property of object mstrProviderServerName. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			mobjWMI = objLocator.ConnectServer(mstrProviderServerName, strNamespace, strUser, strPassword)
		Else
			'UPGRADE_WARNING: Couldn't resolve default property of object mstrProviderServerName. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
			mobjWMI = objLocator.ConnectServer(mstrProviderServerName, strNamespace)
		End If
		
		Connect = True
		mflgConnected = True
		SetMErrors()
		RaiseEvent NewConnectionStatus(True)
		Exit Function
		
ConnectErr: 
		
		Connect = False
		mflgConnected = False
		SetMErrors()
		RaiseEvent NewConnectionStatus(False)
		
	End Function
	
	Private Sub SetMErrors()
		mlngLastError = Err.Number
		mstrLastErrorString = Err.Description
	End Sub
	
	'UPGRADE_NOTE: Class_Initialize was upgraded to Class_Initialize_Renamed. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="A9E4979A-37FA-4718-9994-97DD76ED70A7"'
	Private Sub Class_Initialize_Renamed()
		mflgConnected = False
		'UPGRADE_NOTE: Object mobjWMI may not be destroyed until it is garbage collected. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6E35BFF6-CD74-4B09-9689-3E1A43DF8969"'
		mobjWMI = Nothing
		mstrSiteCode = ""
		mstrSiteServerName = ""
		'UPGRADE_WARNING: Couldn't resolve default property of object mstrProviderServerName. Click for more: 'ms-help://MS.VSCC.v90/dv_commoner/local/redirect.htm?keyword="6A50421D-15FE-4896-8A1B-2EC21E9037B2"'
		mstrProviderServerName = ""
		RaiseEvent NewConnectionStatus(False)
	End Sub
	Public Sub New()
		MyBase.New()
		Class_Initialize_Renamed()
	End Sub
End Class